﻿/****************************** Module Header ******************************\

Copyright (c) Gary Davidson

This source is subject to the Microsoft Public License.
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
All other rights reserved.

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
namespace Savviety.ServiceManager.Facade
{
    public static class SCSMSettings
    {
        private static SCSMIncidentSettings incidentSettings;
        private static SCSMActivitySettings activitySettings;
        private static SCSMChangeManagementSettings changeManagementSettings;


        public static SCSMIncidentSettings IncidentSettings
        {
            get
            {


                if (incidentSettings == null)
                    GetIncidentSettings();

                return incidentSettings;
            }
        }

        public static SCSMActivitySettings ActivitySettings
        {
            get
            {
                if (activitySettings == null)
                    GetActivitySettings();
                return activitySettings;
            }
        }

        public static SCSMChangeManagementSettings ChangeManagementSettings
        {
            get
            {
                if (changeManagementSettings == null)
                    GetChangeManagementSettings();

                return changeManagementSettings;
            }
        }

        private static void GetActivitySettings()
        {

            var @class = SCSMServer.GetSingletonObject(SCSMServer.GetClass("5e04a50d-01d1-6fce-7946-15580aa8681d"));
            if (@class == null)
            {
                throw new InvalidOperationException("Could not retrieve Default Incident Settings");
            }

            activitySettings = new SCSMActivitySettings
                                   {
                                       ManualActivityIDPrefix =
                                           @class[null, "SystemWorkItemActivityManualActivityIdPrefix"].Value.
                                           ToString(),
                                       ReviewActivityIDPrefix =
                                           @class[null, "SystemWorkItemActivityReviewActivityIdPrefix"].Value.
                                           ToString()
                                   };


        }

        private static void GetChangeManagementSettings()
        {
            var @class = SCSMServer.GetSingletonObject(SCSMServer.GetClass("c7fe33bb-9760-3f88-59fc-0951e3221be4"));

            changeManagementSettings = new SCSMChangeManagementSettings
                                           {
                                               ChangeRequestIDPrefix =
                                                   @class[null, "SystemWorkItemChangeRequestIdPrefix"].Value.
                                                   ToString(),
                                               MaxFileAttachmentsCount =
                                                   Convert.ToInt32(@class[null, "MaxFileAttachmentsCount"].Value),
                                               MaxFileAttachmentSizeKB =
                                                   Convert.ToInt32(@class[null, "MaxFileAttachmentSizeinKB"].Value)
                                           };

        }


        private static void GetIncidentSettings()
        {

            var @class = SCSMServer.GetSingletonObject(SCSMServer.GetClass("613c9f3e-9b94-1fef-4088-16c33bfd0be1"));
            if (@class == null)
            {
                throw new InvalidOperationException("Could not retrieve Default Incident Settings");
            }

            incidentSettings = new SCSMIncidentSettings
                                       {
                                           MaxAttachments = Convert.ToInt32(@class[null, "MaxAttachments"].Value),
                                           MaxAttachmentSize = Convert.ToInt32(@class[null, "MaxAttachmentSize"].Value),
                                           PrefixForIdentifier = @class[null, "PrefixForId"].Value.ToString(),
                                           DataRetentionTimeUnit =
                                               Convert.ToInt32(@class[null, "DataRetentionTimeUnit"].Value),
                                           DataRetentionTime = Convert.ToInt32(@class[null, "DataRetentionTime"].Value),
                                           PriorityMatrix = @class[null, "PriorityMatrix"].Value.ToString(),
                                           ResolutionMappings = @class[null, "ResolutionMappings"].Value.ToString()
                                           
                                       };
            // TODO do not know why this is null
            //incidentSettings.DefaultSupportGroup = settings[null, "DefaultSupportGroup"].Value.ToString();

        }

    }
}
